<?php
namespace app\tree\service;
use app\tree\model\MutualModel;
use app\tree\validata\TreeValidata;
use app\my\model\MyFriendModel;
use app\base\service\BaseService;
use think\facade\Log;
//树种植service类
class MutualService extends BaseService 
{
	 protected $mutual ='';
	 protected $tv='';
	 public function __construct()
	 {
	 	parent::__construct();

	 	$this->mutual = new MutualModel;
	 	$this->tv = new TreeValidata;
	 }
	 //树的排行榜treesimg
	 protected function get_ranks($data){
	 	if (!$this->tv->scene('get_rank')->check($data)) {
            return $this->return_code(300,$this->tv->getError());
        }
        $data['page'] = (isset($data['page'])&&!empty($data['page'])) ? $data['page']:1;
		$data['limit'] = (isset($data['limit'])&&!empty($data['limit'])) ? $data['limit']:50;

        //获得用户好友列表
        $friend = new MyFriendModel;
        $where_inf =$this->user['currToken'];
        $frinedlist = $friend->freind_list($this->user['Openid'],$data);
       // $frinedlist=array_unique($frinedlist);
        foreach ($frinedlist as $k => $v) {
        	$where_inf.=','.$v['currToken'];
        }
         
        $res = $this->mutual->get_ranks($data,$where_inf);
        return $this->return_code(200,'成功',$res);
       
	 }
	 //获得能量列表
	 protected function get_energys($data){
	 	if (!$this->tv->scene('get_energys')->check($data)) {
            return $this->return_code(300,$this->tv->getError());
        }
       
        $data['Token'] = (isset($data['Token'])&&!empty($data['Token'])) ? $data['Token'] : $this->user['Token'];
        // $data['Openid'] = (isset($data['Openid'])&&!empty($data['Openid']))? $data['Openid'] : $this->user['Openid'];
  
        $res = $this->mutual->get_energy($data);
        if($res == 0||empty($res)){
        	return $this->return_code(301,'暂时没有能量，明天再来吧');
        }
       return $this->return_code(200,'成功',$res);
	 }

	//摘取树能量 
	protected function pick_energys($data){
		if (!$this->tv->scene('pick_energys')->check($data)) {
            return $this->return_code(300,$this->tv->getError());
        }
        

        $where_tui['treeuserid'] = $data['treeuserid'];
		$tree = $this->mutual->findByAttributes('plant_trees_user',$where_tui,"totalenergy as energy,treesid,token,Userid,openid");
		// //通过树的家庭token找到主创建人的openid
		// $where_user['Token'] = $tree['token'];
		 //判断家庭是否摘取过能量
          $where_token['Token'] = $data['Token'];
          $where_token['passiveToken'] = $tree['token'];
          $where_token['flag'] = $data['flag'];
          $count = $this->mutual->countWhere('plant_trees_record',$where_token);
          if($count>0) 	return $this->return_code(201,'领取过了');
    	//判断用户属性是家人，好友，陌生人还是自己摘取养分
        $perm = $this->permissions($data['Openid']);
     	
        if($perm['code'] ==10004)   return $this->return_code(301,"对不起，权限不足");
  
		$res = $this->mutual->pick_energys($data,$tree,$perm);
		if($res)
		return $this->return_code(200,'成功');
		return $this->return_code();
	}



	//获得飘窗消息
	protected function messageFloats($data){
		if (!$this->tv->scene('messageFloats')->check($data)) {
            return $this->return_code(300,$this->tv->getError());
        }
		$data['token']=$data['token'];
		$message = $this->mutual->get_messageFloats($data);
		return $this->return_code(200,"成功",$message);
	}


	//浇水
	public function waterings($data)
	{
		if (!$this->tv->scene('waterings')->check($data)) {
            return $this->return_code(300,$this->tv->getError());
        }
         $where_tui['token'] = $data['passToken'];//被动浇水的Token
		 $where_tui['Iscomplete'] = 0;
  
		$tree = $this->mutual->findByAttributes('plant_trees_user',$where_tui,"treesid,token,Userid,openid,totalenergy,treeuserid");
		  
		$data['openid']=$this->user['Openid'];
		// $data['energy']=$this->user['energy'];

		//获得浇水发起者的树的id
		 $where_mytree['token'] = $data['Token'];//主动浇水的Token
		 $where_mytree['Iscomplete'] = 0;
		$mytree = $this->mutual->findByAttributes('plant_trees_user',$where_mytree,"treesid,treeuserid,token,totalenergy");
		if($mytree['totalenergy']<20) return $this->return_code(203,"能量不足20g,给自己留点吧");
		$waterings = $this->mutual->waterings($data,$tree,$mytree);
		if($waterings ==1) return $this->return_code(200,"成功",$waterings);
		if($waterings ==2) return $this->return_code(201,"家庭今天浇过三次水了，明天再来吧");
		return $this->return_code(202,"失败");
		
	}

	//通过token 获得家庭能量
	public function get_token_energys($data){
		$where['token'] = $data['Token'];
		$tenergy = $this->mutual->findByAttributes('token_energy',$where,"energyId,currentEnergy,totalEnergy,token");
		return $this->return_code(200,"成功",$tenergy);

	}

	//兑换
	public function exchanges($data){
		$tree_energy = $this->mutual->get_exchanges($data);//获得进度条数据
		return $this->return_code(200,"成功",$tree_energy);
	}

	//获得树的阶段信息
	public function getPhases($data)
	{
		$tree_phase = $this->mutual->getPhases($data);
		return $this->return_code(200,"成功",$tree_phase);
	}
}